package com.yohann.educenter.controller;


import com.yohann.commonutils.jwt.JwtUtils;
import com.yohann.commonutils.result.R;
import com.yohann.commonutils.vo.MemberOrderVo;
import com.yohann.educenter.entity.UcenterMember;
import com.yohann.educenter.entity.vo.RegisterVo;
import com.yohann.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-13
 */
@RestController
@RequestMapping("/ucenter-service")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        String token = memberService.login(member);

        return R.ok().data("token",token);
    }

    /**
     * 注册账号
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 通过token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //通过token获取id
        String id = JwtUtils.getMemberIdByJwtToken(request);

        //通过id获取用户信息
        UcenterMember member = memberService.getById(id);

        if (member == null){
            return R.error().code(23004).message("请先登录！");
        }
        return R.ok().data("userInfo",member);
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("getMemberInfo/{id}")
    public MemberOrderVo getMemberInfo(@PathVariable String id){
        //通过id获取用户信息
        UcenterMember member = memberService.getById(id);
        MemberOrderVo memberOrderVo = new MemberOrderVo();
        BeanUtils.copyProperties(member,memberOrderVo);

        return memberOrderVo;
    }

    /**
     * 查询某一天注册人数
     * @param day
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        int count = memberService.countRegister(day);
        return R.ok().data("count",count);
    }
}

